#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import Any

from cta.interface.action.filter.abstract_filter_action import AbstractFilterAction
from cta.config.cross_section_config.multi_factor_config import MultiFactorConfig
from web.domain.responsibility_chain_dto import ResponsibilityChainDto
from web.models.quant2_commodity_future_filter import Quant2CommodityFutureFilter
from web.manager.log_manager import LogManager
from web.util.commodity_future_code_util import CommodityFutureCodeUtil

Logger = LogManager.get_logger(__name__)

class ExcludeCodeFilterAction(AbstractFilterAction):
    """
    排除不在交易范围内的期货品种的动作
    """

    def exec(self, responsibility_chain_dto: ResponsibilityChainDto = None) -> Any:
        Logger.info("排除不在交易范围内的期货品种")

        quant2_commodity_future_filter_list: list[Quant2CommodityFutureFilter] = self.quant2_commodity_future_filter_dao.find_list(dict(), dict(), list())

        if quant2_commodity_future_filter_list is not None and len(quant2_commodity_future_filter_list):
            for quant2_commodity_future_filter in quant2_commodity_future_filter_list:
                # 如果这个期货品种不在交易范围内，则删除这条记录
                code_str: str = CommodityFutureCodeUtil.code_to_contract_code(quant2_commodity_future_filter.code)
                if code_str.upper() in MultiFactorConfig.Exclude_List:
                    self.quant2_commodity_future_filter_dao.delete(quant2_commodity_future_filter)
                    Logger.info("期货[%s]不在交易范围内，删除这条记录", quant2_commodity_future_filter.code)
        else:
            Logger.warning("表quant2_c_f_filter没有记录")

        super().next(responsibility_chain_dto)